2020-2021 Sunseeker Telemetry and Lighting System
usci_a_spi.c
Go to the documentation of this file.
1 //*****************************************************************************
2 //
3 // usci_a_spi.c - Driver for the usci_a_spi Module.
4 //
5 //*****************************************************************************
6 
7 //*****************************************************************************
8 //
11 //
12 //*****************************************************************************
13 
14 #include "inc/hw_memmap.h"
15 
16 #ifdef __MSP430_HAS_USCI_Ax__
17 #include "usci_a_spi.h"
18 
19 #include <assert.h>
20 
21 bool USCI_A_SPI_initMaster(uint16_t baseAddress, USCI_A_SPI_initMasterParam *param)
22 {
23  //Disable the USCI Module
24  HWREG8(baseAddress + OFS_UCAxCTL1) |= UCSWRST;
25 
26  //Reset OFS_UCAxCTL0 values
27  HWREG8(baseAddress + OFS_UCAxCTL0) &= ~(UCCKPH + UCCKPL + UC7BIT + UCMSB +
28  UCMST + UCMODE_3 + UCSYNC);
29 
30  //Reset OFS_UCAxCTL1 values
31  HWREG8(baseAddress + OFS_UCAxCTL1) &= ~(UCSSEL_3);
32 
33  //Select Clock
34  HWREG8(baseAddress + OFS_UCAxCTL1) |= param->selectClockSource;
35 
36  HWREG16(baseAddress + OFS_UCAxBRW) =
37  (uint16_t)(param->clockSourceFrequency / param->desiredSpiClock);
38 
39  /*
40  * Configure as SPI master mode.
41  * Clock phase select, polarity, msb
42  * UCMST = Master mode
43  * UCSYNC = Synchronous mode
44  * UCMODE_0 = 3-pin SPI
45  */
46  HWREG8(baseAddress + OFS_UCAxCTL0) |= (
47  param->msbFirst +
48  param->clockPhase +
49  param->clockPolarity +
50  UCMST +
51  UCSYNC +
52  UCMODE_0
53  );
54  //No modulation
55  HWREG8(baseAddress + OFS_UCAxMCTL) = 0;
56 
57  return ( STATUS_SUCCESS) ;
58 }
59 
60 void USCI_A_SPI_changeMasterClock(uint16_t baseAddress,
61  USCI_A_SPI_changeMasterClockParam *param)
62 {
63  //Disable the USCI Module
64  HWREG8(baseAddress + OFS_UCAxCTL1) |= UCSWRST;
65 
66  HWREG8(baseAddress + OFS_UCAxBRW) =
67  (uint16_t)(param->clockSourceFrequency / param->desiredSpiClock);
68 
69  //Reset the UCSWRST bit to enable the USCI Module
70  HWREG8(baseAddress + OFS_UCAxCTL1) &= ~(UCSWRST);
71 }
72 bool USCI_A_SPI_initSlave (uint16_t baseAddress,
73  uint8_t msbFirst,
74  uint8_t clockPhase,
75  uint8_t clockPolarity
76  )
77 {
78 
79  //Disable USCI Module
80  HWREG8(baseAddress + OFS_UCAxCTL1) |= UCSWRST;
81 
82  //Reset OFS_UCAxCTL0 register
83  HWREG8(baseAddress + OFS_UCAxCTL0) &= ~(UCMSB +
84  UC7BIT +
85  UCMST +
86  UCCKPL +
87  UCCKPH +
88  UCMODE_3
89  );
90 
91  //Clock polarity, phase select, msbFirst, SYNC, Mode0
92  HWREG8(baseAddress + OFS_UCAxCTL0) |= (clockPhase +
93  clockPolarity +
94  msbFirst +
95  UCSYNC +
96  UCMODE_0
97  );
98 
99  return ( STATUS_SUCCESS) ;
100 }
101 
102 void USCI_A_SPI_changeClockPhasePolarity (uint16_t baseAddress,
103  uint8_t clockPhase,
104  uint8_t clockPolarity
105  )
106 {
107 
108  //Disable the USCI Module
109  HWREG8(baseAddress + OFS_UCAxCTL1) |= UCSWRST;
110 
111  HWREG8(baseAddress + OFS_UCAxCTL0) &= ~(UCCKPH + UCCKPL);
112 
113  HWREG8(baseAddress + OFS_UCAxCTL0) |= (
114  clockPhase +
115  clockPolarity
116  );
117 
118  //Reset the UCSWRST bit to enable the USCI Module
119  HWREG8(baseAddress + OFS_UCAxCTL1) &= ~(UCSWRST);
120 }
121 
122 void USCI_A_SPI_transmitData ( uint16_t baseAddress,
123  uint8_t transmitData
124  )
125 {
126  HWREG8(baseAddress + OFS_UCAxTXBUF) = transmitData;
127 }
128 
129 uint8_t USCI_A_SPI_receiveData (uint16_t baseAddress)
130 {
131  return ( HWREG8(baseAddress + OFS_UCAxRXBUF)) ;
132 }
133 
134 void USCI_A_SPI_enableInterrupt (uint16_t baseAddress,
135  uint8_t mask
136  )
137 {
138  HWREG8(baseAddress + OFS_UCAxIE) |= mask;
139 }
140 
141 void USCI_A_SPI_disableInterrupt (uint16_t baseAddress,
142  uint8_t mask
143  )
144 {
145  HWREG8(baseAddress + OFS_UCAxIE) &= ~mask;
146 }
147 
148 uint8_t USCI_A_SPI_getInterruptStatus (uint16_t baseAddress,
149  uint8_t mask
150  )
151 {
152  return ( HWREG8(baseAddress + OFS_UCAxIFG) & mask );
153 }
154 
155 void USCI_A_SPI_clearInterrupt (uint16_t baseAddress,
156  uint8_t mask
157  )
158 {
159  HWREG8(baseAddress + OFS_UCAxIFG) &= ~mask;
160 }
161 
162 void USCI_A_SPI_enable (uint16_t baseAddress)
163 {
164  //Reset the UCSWRST bit to enable the USCI Module
165  HWREG8(baseAddress + OFS_UCAxCTL1) &= ~(UCSWRST);
166 }
167 
168 void USCI_A_SPI_disable (uint16_t baseAddress)
169 {
170  //Set the UCSWRST bit to disable the USCI Module
171  HWREG8(baseAddress + OFS_UCAxCTL1) |= UCSWRST;
172 }
173 
174 uint32_t USCI_A_SPI_getReceiveBufferAddressForDMA (uint16_t baseAddress)
175 {
176  return ( baseAddress + OFS_UCAxRXBUF );
177 }
178 
179 uint32_t USCI_A_SPI_getTransmitBufferAddressForDMA (uint16_t baseAddress)
180 {
181  return ( baseAddress + OFS_UCAxTXBUF );
182 }
183 
184 uint8_t USCI_A_SPI_isBusy (uint16_t baseAddress)
185 {
186  //Return the bus busy status.
187  return (HWREG8(baseAddress + OFS_UCAxSTAT) & UCBUSY);
188 }
189 
190 
191 #endif
192 //*****************************************************************************
193 //
196 //
197 //*****************************************************************************
uint8_t transmitData
MPU_initThreeSegmentsParam param
#define HWREG8(x)
Definition: hw_memmap.h:41
#define HWREG16(x)
Definition: hw_memmap.h:39
#define STATUS_SUCCESS
Definition: hw_memmap.h:22